#!/usr/bin/perl
if(@ARGV != 3){
	print "Usage $0 repositoryTrunkPath repositoryBranchPath fileName\n";
	print "Example:\n", "$0 http://cscw.fudan.edu.cn/svn/test/trunk http://cscw.fudan.edu.cn/svn/test/branch a\n\n";
        die "WARNNING: The program needs three arguments, please input two SVN URLs and the file name.\n";
}
$trunkpath = $ARGV[0];
$branchpath = $ARGV[1];
$fileName = $ARGV[2];

print 'check out files from SVN repository ....',"\n";
@lines = `svn log -q --stop-on-copy --incremental $branchpath/$fileName`;
@segments = split(/\|/, $lines[-1]);
$revision = substr($segments[0],1,length($segments[0])-2);
print "check out base file $fileName.base\n";
`svn cat -r $revision $branchpath/$fileName > $fileName.base`;
print "check out trunk file $fileName.trunk\n";
`svn cat $trunkpath/$fileName > $fileName.trunk`;
print "check out branch file $fileName.branch\n";
`svn cat $branchpath/$fileName > $fileName.branch`;
print "Doing 3-way merge ...\n";
system("clear");
#############################
system("diff3 -m --strip-trailing-cr $fileName.trunk $fileName.base $fileName.branch |colordiff.pl|less -R");
############################
print "Writing merged result to $fileName.merged ...\n\n";
system("diff3 -m --strip-trailing-cr $fileName.trunk $fileName.base $fileName.branch > $fileName.merged");
############################
##Print additional Information
#############################
print "\033[0;0m!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
print "\033[0;0mNote: If there is no overlapping conflicts, please use '\033[1;31mdiff -b $fileName.base $fileName.trunk|colordiff.pl|less -R\033[0;0m' and '\033[1;31mdiff -b $fileName.base $fileName.branch|colordiff.pl|less -R\033[0;0m' to view the nonoverlapping changes.\n";
print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
